﻿using Pacs.Services.Soa;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pacs.Services.Soa
{
    public class ServiceBase : ServiceStack.ServiceInterface.Service
    {
        private StringBuilder _AccessSnapshot = new StringBuilder();
        private DateTime _BeginTime;
        private DateTime _EndTime;
        private string _RequestString = string.Empty;
        public ServiceBase()
        {
           
        }
      
        public DTOResponse<T> Fail<T>(List<string> msgs)
        {
            //EndExecute();
            return new DTOResponse<T>(false, msgs);
        }
        public DTOResponse<T> Fail<T>(string msg)
        {
            //EndExecute();
            return new DTOResponse<T>(false, msg);
        }
        public DTOResponse<T> Success<T>(T body)
        {
            //EndExecute();
            return new DTOResponse<T>(body);
        }
        public object DTORequest
        {
            get;
            set;
        }
        public RouteAttribute RouteAttr { get; set; }
       // public DTORequestAttribute RequestAttr { get; set; }
        //protected R EntityToResponse<R, E>(E entity, Action<R> action = null)
        //{
        //    //return ServiceStack.OrmLite.EntityToResponse(entity, action);
        //}
        //protected List<R> EntitiesToResponseList<R, E>(List<E> entities, Action<R> action = null)
        //{
        //   // return ServiceStack.OrmLite.EntitiesToResponseList(entities, action);
        //}
        //protected E RequestToEntity<R, E>(R request, Action<E> action = null)
        //{
        //   // return OrmLite.RequestToEntity(request, action);
        //}
        //protected List<E> RequestToEntitiesTo<R, E>(List<R> requests, Action<E> action = null)
        //{
        //    return OrmLite.RequestToEntitiesTo(requests, action);
        //}
        //protected bool CheckRequest<T>(DTORequest<T> iRequest)
        //{
        //    var reqattrs = iRequest.GetType().GetCustomAttributes(typeof(DTORequestAttribute), true);
        //    var rtattrs = iRequest.GetType().GetCustomAttributes(typeof(RouteAttribute), true);
        //    if (reqattrs.Length <= 0)
        //    {
        //        return false;
        //    }
        //    if (rtattrs.Length <= 0)
        //    {
        //        return false;
        //    }
        //    this.RequestAttr = (DTORequestAttribute)reqattrs[0];
        //    this.RouteAttr = (RouteAttribute)rtattrs[0];
        //    this.DTORequest = iRequest;
        //    this._RequestString = GetRequestString<T>();
        //    BeginExecute();
        //    iRequest.DoAssert();
        //    return iRequest.IsAllPassed();
        //}
        //private string GetRequestString<T>()
        //{
        //    try
        //    {
        //        var s = new ServiceStack.Text.JsonSerializer<DTORequest<T>>();
        //        return s.SerializeToString((DTORequest<T>)this.DTORequest);
        //    }
        //    catch (Exception ex)
        //    {
        //        var sign = ex.Message;
        //        return string.Empty;
        //    }
        //}
        protected void BeginExecute()
        {
            this._BeginTime = DateTime.Now;
            _AccessSnapshot.AppendLine("");
            _AccessSnapshot.AppendLine("------------------------------------------------------------------------------------------------------------------------");
            //_AccessSnapshot.AppendLine(string.Format("线程编号：{0}", threadid));
            _AccessSnapshot.AppendLine(string.Format("接口:{0}", this.RouteAttr.Path));
            _AccessSnapshot.AppendLine(string.Format("参数:{0}", this._RequestString));
            _AccessSnapshot.AppendLine(string.Format("开始时间:{0}", this._BeginTime.ToString("yyyy-MM-dd HH:mm:ss.fff")));
        }
    //    protected void EndExecute()
    //    {
    //        #region 处理未提交的事务
    //        if (TransactionPool.IsTransting())
    //        {
    //            TransactionPool.RollBack();
    //            logger.Fatal(new CoreEx(Error.DB_TRANS_NOTEND));
    //        }
    //        #endregion
    //        this._EndTime = DateTime.Now;
    //        var timespan = (this._EndTime - this._BeginTime).TotalSeconds;
    //        _AccessSnapshot.AppendLine(string.Format("结束时间:{0}", this._EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff")));
    //        _AccessSnapshot.AppendLine(string.Format("耗时:{0}", timespan.ToString()));
    //        this.logger.Info(this._AccessSnapshot.ToString());
    //    }
    //}
    }
}
